package com.jshoperxms.components.netease.sms;

import com.jshoperxms.components.netease.CheckSumBuilder;
import com.jshoperxms.components.netease.JPNeteaseKey;
import com.jshoperxms.controller.utils.json.GsonJson;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by sdywcd on 2018/3/23.
 * Des:短信发送
 */
public class NeteaseSmsValidateCodeTool {


    //随机数
    private static final String NONCE="00112233445566778899";

    //验证码长度，范围4～10，默认为4
    private static final String CODELEN="4";



    /**
     * 发送老师绑定手机号的验证码请求
     * @param mobile
     * @return
     * @throws Exception
     */
    public static ValidateCodeBean sendBindTeacherMobileCode(String mobile) throws Exception{
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(SmsUrl.SMS_SERVER_URL);
        String curTime=String.valueOf((new Date()).getTime()/1000L);
        String checkSum= CheckSumBuilder.getCheckSum(JPNeteaseKey.SECRET,NONCE,curTime);
        // 设置请求的header
        httpPost.addHeader("AppKey", JPNeteaseKey.APPKEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("templateid", SmsValidateTemplate.BINDTEACHERTEMPLATEID));
        nvps.add(new BasicNameValuePair("mobile", mobile));
        nvps.add(new BasicNameValuePair("codeLen", CODELEN));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        CloseableHttpResponse response = httpclient.execute(httpPost);
        ValidateCodeBean smsBean= GsonJson.parsejsonToObject(EntityUtils.toString(response.getEntity()),ValidateCodeBean.class);
        return smsBean;
    }




    /**
     * 发送忘记密码时获取验证码
     * @param mobile
     * @return
     * @throws Exception
     */
    public static ValidateCodeBean sendRestPwdValidateCode(String mobile) throws Exception{
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(SmsUrl.SMS_SERVER_URL);
        String curTime=String.valueOf((new Date()).getTime()/1000L);
        String checkSum= CheckSumBuilder.getCheckSum(JPNeteaseKey.SECRET,NONCE,curTime);
        httpPost.addHeader("AppKey", JPNeteaseKey.APPKEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("templateid", SmsValidateTemplate.RESTPWDTEMPLATEID));
        nvps.add(new BasicNameValuePair("mobile", mobile));
        nvps.add(new BasicNameValuePair("codeLen", CODELEN));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        CloseableHttpResponse response = httpclient.execute(httpPost);
        ValidateCodeBean smsBean=GsonJson.parsejsonToObject(EntityUtils.toString(response.getEntity()),ValidateCodeBean.class);
        return smsBean;
    }

    /**
     * 发送验证码
     * @param mobile
     * @return
     * @throws Exception
     */
    public static ValidateCodeBean sendRegisterValidateCode(String mobile) throws Exception{
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(SmsUrl.SMS_SERVER_URL);
        String curTime=String.valueOf((new Date()).getTime()/1000L);
        String checkSum= CheckSumBuilder.getCheckSum(JPNeteaseKey.SECRET,NONCE,curTime);
        httpPost.addHeader("AppKey", JPNeteaseKey.APPKEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("templateid", SmsValidateTemplate.STUDENTREGISTERTEMPLATEID));
        nvps.add(new BasicNameValuePair("mobile", mobile));
        nvps.add(new BasicNameValuePair("codeLen", CODELEN));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        CloseableHttpResponse response = httpclient.execute(httpPost);
        ValidateCodeBean smsBean=GsonJson.parsejsonToObject(EntityUtils.toString(response.getEntity()),ValidateCodeBean.class);
        return smsBean;
    }




    /**
     * 验证短信验证码
     * @param mobile
     * @param code
     * @return
     * @throws Exception
     */
    public static int verifycode (String mobile,String code) throws Exception{
        String SERVER_URL="https://api.netease.im/sms/verifycode.action";
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(SERVER_URL);
        String curTime=String.valueOf((new Date()).getTime()/1000L);
        String checkSum= CheckSumBuilder.getCheckSum(JPNeteaseKey.SECRET,NONCE,curTime);
        httpPost.addHeader("AppKey", JPNeteaseKey.APPKEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("mobile", mobile));
        nvps.add(new BasicNameValuePair("code",code));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        CloseableHttpResponse response = httpclient.execute(httpPost);
        ValidateCodeBean smsBean=GsonJson.parsejsonToObject(EntityUtils.toString(response.getEntity()),ValidateCodeBean.class);
        return smsBean.getCode();
    }


}
